<?php
namespace app\admin\controller;
use app\admin\model\system\Administrator;
use think\captcha\Captcha;
use think\Controller;
use think\facade\Cookie;
use think\facade\Validate;
use think\Request;

class Login  extends Controller
{
    protected $middleware = ["ViewInitMiddeware"];
    public $model ;

    public function initialize()
    {
        parent::initialize();
        if(get_user()) $this->redirect("admin/index/index");
        $this->model = new Administrator();
    }

    /**
     * Notes:登陆主页
     * User: chenping
     * email: 375129701@qq.com
     * param Request $request
     * return mixed
     */
    public function index(Request $request)
    {
        $system = get_option("system_setting");

        $system['version'] = get_version();

        if($request->isAjax() && $request->isPost())
        {
            $input = $request->post();

            // 检测输入的验证码是否正确，$value为用户输入的验证码字符串
            if(!cmf_captcha_check($input['verifyCode']))
            {
                $this->error("验证码错误",url('admin/login/index'));
            }

            $adminUser = $this->model->whereOr("username|phone|email",$input['username'])
                /*->whereOr("phone",$input['username'])
                ->whereOr("email",$input['username'])*/
                ->where("is_admin",1)
                ->find();

            if($adminUser)
            {
                //cmf_compare_password($adminUser['password'],$input['password'])
                if(PasswordVerify($input['password'],$adminUser['password']))
                    //if(cmf_compare_password($adminUser['password'],$input['password']) )
                {
                    $loginTime = time();
                    $loginIp   = get_client_ip();
                    $loginData = [
                        "last_login_ip"   => $loginIp,
                        "login_ip"        => $loginIp,
                        "login_time"      => $loginTime,
                        "last_login_time" => $loginTime,
                    ];
                    //上次登陆ip
                    if(!$adminUser['login_ip'])
                    {
                        $loginData['last_login_ip'] = $adminUser['login_ip'];
                    }
                    //上次登录时间
                    if(!$adminUser['login_time'])
                    {
                        $loginData['last_login_time'] = $adminUser['login_time'];
                    }
                    //登录次数加 1
                    $this->model->where("id",$adminUser['id'])->setInc("login_times",1);

                    $res = $this->model->where("id",$adminUser['id'])->update($loginData);

                    //生成token
                    cmf_generate_user_token($adminUser["id"], 'web');
                    if($res)
                    {
                        //存储用户信息
                        Cookie::set('adminUser',$adminUser);
                        $this->success('登录成功',url('admin/index/index'));
                    }else{
                        $this->error('登录失败',url('admin/login/index'));
                    }
                }else{
                    $this->error("密码错误",url('admin/login/index'));
                }
            }else{
                $this->error("账户不存在",url('admin/login/index'));
            }
        }
        $this->assign([
            "system" => $system
        ]);
        return $this->fetch();
    }
}